package de.zainodis.commons.component.ui.widget;

import java.util.Locale;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.zainodis.commons.R;
import de.zainodis.commons.model.CurrencyAmount;
import de.zainodis.commons.utils.RegexUtils;

/**
 * Displays values before and after the decimal point in different
 * {@link EditText}s.
 * 
 * @author fzarrai
 * 
 */
public class SplitCurrencyField extends LinearLayout {

   public SplitCurrencyField(Context context) {
	 super(context);
	 setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	 View.inflate(context, R.layout.w_split_currency_field, this);
	 setCurrencyConstants();
	 addValidators();
   }

   public SplitCurrencyField(Context context, AttributeSet attrs) {
	 super(context, attrs);
	 setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	 View.inflate(context, R.layout.w_split_currency_field, this);
	 setCurrencyConstants();
	 addValidators();
   }

   protected void setCurrencyConstants() {
	 TextView decimalSeparator = (TextView) findViewById(R.id.w_split_currency_field_decimal_separator);
	 decimalSeparator.setText(String.valueOf(CurrencyAmount.getDecimalSeparator(getLocale())));
	 TextView currencySymbol = (TextView) findViewById(R.id.w_split_currency_field_currency_symbol);
	 currencySymbol.setText(CurrencyAmount.getCurrencySymbol(getLocale()));
   }

   protected void addValidators() {
	 final EditText left = (EditText) findViewById(R.id.w_split_currency_field_left);
	 left.addTextChangedListener(new TextWatcher() {

	    @Override
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    }

	    @Override
	    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	    }

	    @Override
	    public void afterTextChanged(Editable s) {
		  validateInput(left.getText().toString(), left);
	    }
	 });
	 final EditText right = (EditText) findViewById(R.id.w_split_currency_field_right);
	 left.addTextChangedListener(new TextWatcher() {

	    @Override
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    }

	    @Override
	    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	    }

	    @Override
	    public void afterTextChanged(Editable s) {
		  validateInput(right.getText().toString(), right);
	    }
	 });
   }

   protected void validateInput(String input, EditText target) {
	 if (!RegexUtils.matchesExactly(RegexUtils.REGEX_DIGIT, input)) {
	    target.setError(String.format(getContext().getString(R.string.invalid_amount), input));
	 } else {
	    target.setError(null);
	 }
   }

   public CurrencyAmount getAmount() {
	 // Build the amount by gathering the values from the two fields
	 EditText left = (EditText) findViewById(R.id.w_split_currency_field_left);
	 EditText right = (EditText) findViewById(R.id.w_split_currency_field_right);

	 if (left.getError() != null || right.getError() != null) {
	    // TODO indicate the field is invalid
	 }

	 String leftText = left.getText() != null ? left.getText().toString().trim() : "0";
	 String rightText = right.getText() != null ? right.getText().toString().trim() : "0";

	 return new CurrencyAmount(String.format("%s%s%s", leftText,
		  CurrencyAmount.getDecimalSeparator(getLocale()), rightText), getLocale());
   }

   /**
    * Subclasses that wish to supply custom {@link Locale}s should override this
    * method.
    */
   protected Locale getLocale() {
	 return Locale.getDefault();
   }
}
